package com.wushijia.designpatterns.single;

/**
 * @author 92090,使用holder模式创建单例，保证了懒加载，线程安全
 */
public final class HoldSingleTest implements Runnable{

  private HoldSingleTest() {
  }

  /**
   * 在静态内部类中持有HoldSingleTest的实例，并且可被直接初始化
   */
  private static class SingleHolder {

    private static final HoldSingleTest instance = new HoldSingleTest();

    //调用getInstance方法，事实上是获得Holder的instance静态属性
    private static HoldSingleTest getInstance() {
      return SingleHolder.instance;
    }
  }

  @Override
  public void run() {
    System.out.println(SingleHolder.getInstance());
  }

  public static void main(String[] args) {
    Runnable runnable = new HoldSingleTest();
    Thread singleTest1 = new Thread(runnable);
    Thread singleTest2 = new Thread(runnable);
    singleTest1.start();
    singleTest2.start();
  }
}
